/*
 * ModuleController.java
 *
 * Copyright 2012 the original author or authors(ninglong).
 *
 * Licensed under the org.geek.module.web, Version 1.0 (the "License");
 *
 */
package org.geek.module.web;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.geek.common.base.BaseController;
import org.geek.common.entity.Criteria;
import org.geek.common.entity.ExtCheckTree;
import org.geek.common.entity.ExtGridReturn;
import org.geek.common.entity.ExtPager;
import org.geek.common.entity.ExtReturn;
import org.geek.common.entity.ExtTree;
import org.geek.common.entity.Menu;
import org.geek.common.util.TreeUtil;
import org.geek.domain.Module;
import org.geek.module.service.IModuleService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * <p></p>
 * @author ninglong bhd2973@qq.com
 * @version V1.0
 */
@Controller
@RequestMapping("/module/")
public class ModuleController extends BaseController{
	
	private static final Logger logger = LoggerFactory.getLogger(ModuleController.class);
	
	@Resource
	private IModuleService moduleService;
	
	@RequestMapping(value="initPage.jhtml",method=RequestMethod.GET)
	@Override
	public String initPage(HttpServletRequest request, ModelMap model) {
		model.put("parentId", request.getParameter("parentId"));
		log(request,logger,"访问模块管理");
		return "module/module";
	}
	
	@RequestMapping(value="getDataList.jhtml",method=RequestMethod.POST)
	@ResponseBody
	public Object getDataList(HttpServletRequest request,ExtPager pager){
		Criteria criteria = new Criteria();
		List<Module> modules = moduleService.getModuleByCriteria(criteria);//查询出所有的
		
		String id = request.getParameter("id");
		if(StringUtils.isNotEmpty(id)){
			criteria.put("id", id);
		}
		int total = moduleService.countModuleByCriteria(criteria);
		// 设置分页信息
		if (pager.getLimit() != null && pager.getStart() != null) {
			criteria.setEnd(pager.getLimit() + pager.getStart());
			criteria.setStart(pager.getStart());
		}
		List<Module> list = moduleService.getModuleByCriteria(criteria);//带条件查询
		for(Module module : list){
			for(Module m : modules){
				if(module.getParentId()==m.getModuleId()){
					module.setParentText(m.getModuleText());
					break;
				}
			}
		}
		return new ExtGridReturn(total, list);
	}
	
	@RequestMapping(value="getModuleTree.jhtml",method = RequestMethod.POST)
	@ResponseBody
	public List<ExtTree> getModuleTree(HttpServletRequest request){
		String moduleId = request.getParameter("id");
		List<Menu> list = moduleService.getModules2Menu(0);
		return TreeUtil.getTree(list, Integer.valueOf(moduleId));
	}
	
	@RequestMapping(value="getModuleCheckBoxTree.jhtml",method = RequestMethod.POST)
	@ResponseBody
	public List<ExtCheckTree> getModuleCheckBoxTree(HttpServletRequest request){
		String moduleId = request.getParameter("id");
		List<Menu> list = moduleService.getModules2Menu(0);
		List<Menu> menuHasAuth = moduleService.getModules2Menu(this.getUser(request).getUserId());
		return TreeUtil.getTreeHasCheckBox(list, Integer.valueOf(moduleId),menuHasAuth);
	}
	
	@RequestMapping(value="addModule.jhtml",method=RequestMethod.POST)
	@ResponseBody
	public Object addModule(HttpServletRequest request,Module module){
		boolean success = true;
		
		if(StringUtils.isEmpty(module.getModuleName()))success = false;
		
		if(success && moduleService.addModule(module)<=0){
			success = false;
		}
		String msg = success?"模块添加成功！":"模块添加失败！";
		log(request,logger,msg);
		return new ExtReturn(success, msg);
	}
	
	@RequestMapping(value="modModule.jhtml",method=RequestMethod.POST)
	@ResponseBody
	public Object modModule(HttpServletRequest request,Module module){
		boolean success = true;
		
		if(StringUtils.isEmpty(module.getModuleName()))success = false;
		
		if(success && moduleService.modModule(module)<=0){
			success = false;
		}
		String msg = success?"模块修改成功！":"模块修改失败！";
		log(request,logger,msg);
		return new ExtReturn(success, msg);
	}
	
	@RequestMapping(value="delModuleByModuleId.jhtml",method=RequestMethod.POST)
	@ResponseBody
	public Object delModuleByModuleId(HttpServletRequest request,int id){
		boolean success = true;
		if(moduleService.delModuleByModuleId(id)<=0){
			success = false;
		}
		String msg = success?"模块删除成功！":"模块删除失败！";
		log(request,logger,msg);
		return new ExtReturn(success, msg);
	}
}
